<?php
/**
 * 会员消息逻辑处理模块
 * User：LLW
 * Data：2020-03-13
 */

namespace app\caidongweb\model;

class UserLogModel extends BaseModel
{
    protected $table = 'cd_user_log';

    /**
     * 首页消息
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        $where = [];
        $sta = strtotime(input('start'));
        $end = strtotime(input('end'));
        if($sta && $end){
            $where['ul_time'] = array('between',"$sta,$end");
        }
        input('key') ? $where[input('city')] = input('key') : false;
        input('type') ? $where['ul_state'] = input('type') : false;
        $list = $this->alias('l')
            ->join('cd_user u','l.ul_uid=u.us_uid')
            ->where($where)->field('l.*,u.us_name')
            ->order('ul_time desc')
            ->paginate(50,false,['query'=>request()->param()]);
        return $list;
    }

    /**
     * 后台站内发送消息
     * @return array
     */
    public function getAdd()
    {
        $data = input('post.');
        $user = db('user')->where('us_phone',$data['phone'])->find();
        if(empty($user)){
            return $this->returnData(-1,'发送用户不存在');
        }
        $res = $this->insert([
            'ul_uid'=>$user['us_uid'],
            'ul_desc'=>$data['desc'],
            'ul_time'=>time(),
        ]);
        if($res){
            accountLog(2,1,session('sname'),'给用户:'.$data['phone'].'发送:'.$data['desc']);
            return $this->returnData(1,'发送成功');
        }else{
            return $this->returnData(-1,'哎呀！网络好像在开小差，请刷新网页重新试一下');
        }
    }
}